package com.toplabs.unashu.controller;

import com.toplabs.unashu.annotation.ControllerEndpoint;
import com.toplabs.unashu.common.response.CommonResponse;
import com.toplabs.unashu.dto.PageDto;
import com.toplabs.unashu.dto.SearchLog;
import com.toplabs.unashu.entity.LoginLog;
import com.toplabs.unashu.exception.ServiceException;
import com.toplabs.unashu.service.LoginLogService;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author toplabs
 * @since 2022/11/21
 * <p>This java file was created by toplabs in 2022/11/21.
 * The following is the description information about this file:</p>
 * <p>description:前端控制器</p>
 */
@CrossOrigin
@RestController
public class LoginLogController {

    private final LoginLogService loginLogService;

    public LoginLogController(LoginLogService loginLogService) {
        this.loginLogService = loginLogService;
    }

    /**
     * 登入日志列表
     *
     * @param searchLog SearchLog
     * @return Result
     */
    @GetMapping("/logs/login")
    public CommonResponse<PageDto<LoginLog>> getLoginLogList(SearchLog searchLog) {
        PageDto<LoginLog> logList = loginLogService.getLoginLogList(searchLog);
        return CommonResponse.success(logList);
    }

    /**
     * 删除登入日志
     *
     * @param id Long
     * @return Result
     * @throws ServiceException Exception
     */
    @DeleteMapping("/log/login/{id}")
    @ControllerEndpoint(operation = "根据ID删除登入日志", exceptionMessage = "删除登入日志失败")
    public CommonResponse<String> delete(@PathVariable("id") Long id) throws ServiceException {
        loginLogService.delete(id);
        return CommonResponse.success("删除登入日志成功！");
    }

    /**
     * 批量删除登入日志
     *
     * @param ids String
     * @return Result
     */
    @DeleteMapping("/log/login/batch/{ids}")
    @ControllerEndpoint(operation = "批量删除登入日志", exceptionMessage = "批量删除登入日志失败")
    public CommonResponse<String> batchDelete(@PathVariable("ids") String ids) {
        List<Long> list = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        loginLogService.batchDelete(list);
        return CommonResponse.success("批量删除登入日志成功！");
    }
}